package cn.edu.neusoft.zengweidong707.neufood.model;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.listener.ListListener;
import cn.edu.neusoft.zengweidong707.neufood.listener.ZwdListener;
import cn.edu.neusoft.zengweidong707.neufood.service.ResService;
import cn.edu.neusoft.zengweidong707.neufood.service.Server;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Lucio on 2017/11/9.
 */

public class RetrofitBaseModel {
    protected ResService service;
    protected Retrofit retrofit;

    protected Retrofit getRetrofit() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Server.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        return retrofit;

    }

    protected ResService getService() {
        service = retrofit.create(ResService.class);
        return service;
    }

    protected <T> void callenqueue(Call<T> call, final ZwdListener<T> l) {
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    protected <T> void callenqueue(Call<List<T>> call, final ListListener<T> list) {
        call.enqueue(new Callback<List<T>>() {
            @Override
            public void onResponse(Call<List<T>> call, Response<List<T>> response) {
                list.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<T>> call, Throwable t) {
                list.onFail(t.toString());
            }
        });
    }
}
